Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
JavaScript arrays and understanding arr[arr.length]

What is meant by arr[arr.length]. I mean what it really means or what it really does, I cannot understand the logic of how it works. Can anybody explain?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This statement gets the index of the last index of the array plus one. It is used to append items to an array. Equivalent to array.push().

var fruits = ["apple", "orange", "banana", "pear"];
// appends grapes to the array
fruits[fruits.length] = "grapes";
console.log(fruits);

For further reading, check out W3School's page on array methods.

about 4 years ago · Juan Pablo Isaza Relatório

0

arr.length is the length of the array.

arr[arr.length] is accessing by index the array length (out of bounds as length starts at 1, not index 0).

example:

const test = [1,2,3]
test[test.length]
undefined
test[test.length-1]
3
about 4 years ago · Juan Pablo Isaza Relatório

0

arr.length means the length of the array that spans from index 0 to arr.length - 1.

For instance if you got arr = ["a", "b", "c"], arr.length is 3.

arr[0] is "a", arr[1] is "b" and arr[2] is "c".

arr[3] does not exist, yet. but if you do arr[arr.length] = "d", you obtain ["a", "b", "c", "d"].

This is an odd construction and usually, one should simply write arr.push("d").

const arr = ["a", "b", "c"];
console.log(arr.length, arr[0], arr[1], arr[2], arr[3]);
arr[arr.length] = "d";
console.log(arr);
arr.push("e");
console.log(arr);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda